package com.zxy;

import com.zxy.Student;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[]args){
        //集合只能存储引用数据类型
        TreeSet<Integer>set1 = new TreeSet<>();
        //1---》包装成Integer对象---》自动装箱
        set1.add(5);
        set1.add(4);
        set1.add(3);
        set1.add(2);
        set1.add(1);
        for (Integer integer : set1){
            System.out.println(integer);
        }
//        System.out.println(set1);
    }
    public static void Test1(){
        TreeSet<Student> set = new TreeSet<>();

        set.add(new Student("夏春丽",21));
        set.add(new Student("郑巧",90));
        set.add(new Student("钟焮玥",18));
        set.add(new Student("杨斯淇",92));
        set.add(new Student("张淇",91));
        for(Student student: set){
            System.out.println(student.getName() + "---"+student.getName());
        }
    }

    public static void test2(){
        //匿名内部类对象
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                //正序，o1-o2
//                return o1.getAge()-o2.getAge();
//            }
//        };
        //lambda表达式
        Comparator<Student> comparator = (o1, o2)->o1.getAge()-o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);

        set.add(new Student("夏春丽",21));
        set.add(new Student("郑巧",90));
        set.add(new Student("钟焮玥",18));
        set.add(new Student("杨斯淇",92));
        set.add(new Student("张淇",91));

        for (Student student : set){
            System.out.println(student.getName()+"---"+student.getAge());
        }


    }


    //可变参数必须是形参的一个
    public static int sum(int...a){
        int total = 0;
        for (int i =0;i< a.length;i++) {
            total+= a[i];
        }
        return total;
    }
}